"""
__author__ = 'heyanmin'
__time__ = '2021/6/6 4:23 下午'
"""
import requests

from api_demo.api.work_api import WorkApi

class TagApi(WorkApi):

    # 获取标签
    def search(self):
        data = {
            "url":"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list",
            "method":"post",
            "params":{"access_token": self.token},
            "json":{}
        }
        r = self.request(data)
        return r

    # 添加标签
    def add_tags(self,group_name, tag_name,**kw):
        # 如果传入的是大的json
        if "json" in kw:
            json_data = kw['json']
        else:
            json_data = {
                "group_name":group_name,
                "tag": [{"name": tag_name,}]
            }
        data = {
            "url":"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag",
            "method":"post",
            "params":{"access_token": self.token},
            "json":json_data
        }
        r = self.request(data)
        return r


    # 删除标签
    def delete_tags(self, tag_id):
        r = requests.post(
            "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag",
            params={"access_token": self.token},
            json={
                "tag_id": tag_id
            }
        )
        return r

    # 清理数据
    def clear(self):
        r = self.search()
        tagid_list = [tag["id"] for group in r.json()["tag_group"] for tag in group["tag"]]
        r = self.delete_tags(tagid_list)
        return r